package com.yunc.modules.shop.queue;

import com.yunc.base.queue.disruptor.AbstractEventHandler;
import com.yunc.base.queue.disruptor.Event;
import com.yunc.base.queue.disruptor.Handler;
import com.yunc.base.queue.redis.MsgReceiver;
import com.yunc.base.utils.SnowFlake;
import com.yunc.base.utils.SpringContextUtils;
import com.yunc.modules.shop.entity.ShopRandomCode;
import com.yunc.modules.shop.service.IShopRandomCodeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Handler
public class MessageEventHandler extends AbstractEventHandler<MessageEvent> {

    private static final Logger logger = LoggerFactory.getLogger(MsgReceiver.class);
    private static final SnowFlake SNOW_FLAKE = new SnowFlake(9, 12);

	@Override
    public boolean filter(Event event) {
        if (event.getValue().getClass() == MessageEvent.class) {
            return true;
        }
        return false;
    }
	
	@Override
	public void handler(MessageEvent obj) {
        logger.info("消息接收:" + obj);
        IShopRandomCodeService shopRandomCodeService = (IShopRandomCodeService)SpringContextUtils.getBean("shopRandomCodeService");
        // 消息发生
        ShopRandomCode entity = new ShopRandomCode();
        // 采用雪花算法生成订单编号
        String code = String.valueOf(SNOW_FLAKE.nextId());
        entity.setCode(code);
        shopRandomCodeService.save(entity);
        System.out.println(obj);
	}

}
